﻿Imports MirEvo.MessageBoxes
Imports MirEvo.Windows
Imports MirEvo.GUI
Imports MirEvo.Mouse

Namespace Belt

    Module mBelt

        Private currentCell As Integer
        Private arrBeltItems(5) As ItemInfo

        Const cellstep As Integer = 38
        Const cellsize As Integer = 32

        Private isVisible As Boolean
        Private isVertical As Boolean

        Private WithEvents winBelt As GUI.MirWindow

        Public WithEvents btnClose As GUI.MirButton
        Public WithEvents btnRotate As GUI.MirButton



        Public Sub Init()

            winBelt = New MirWindow("winBelt", 0, 0, 274, 50, True)

            btnClose = New MirButton(winBelt, "btnClose", 4, 7, 0, ImgLibs.GameInter, 0, 323, 323, "Close", False)
            btnRotate = New MirButton(winBelt, "btnRotate", 4, 28, 0, ImgLibs.GameInter, 0, 324, 324, "Rotate", False)

        End Sub

        Public Sub Shutdown()

            Hide()

        End Sub

        Public Sub Toggle()

            Visible = Not isVisible

        End Sub

        Public Property Visible() As Boolean
            Get
                Return isVisible
            End Get
            Set(ByVal value As Boolean)
                isVisible = value
                If isVisible Then
                    Show()
                Else
                    Hide()
                End If
            End Set
        End Property

        Private Sub Show()

            Windows.AddWindow(winBelt, mWindows.WindowType.Normal)

        End Sub

        Private Sub Hide()

            Windows.RemoveWindow(winBelt)

        End Sub

        Private Sub winBelt_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles winBelt.DrawWindow

            If mode = Globals.DrawingMode.Normal Then

                With Libs.GameInter

                    If Not isVertical Then

                        .Image(321).Draw(x - 118, y - 7, z)

                    Else

                        .Image(322).Draw(x, y, z)

                    End If

                End With

                Dim dx As Integer, dy As Integer

                If isVertical Then

                    dx = 9
                    dy = 26

                    For i As Integer = 0 To 5

                        With Libs.Inventory.Image(arrBeltItems(i).Base.INT_LOOKS)
                            .Draw(x + dx + .Header.OffsetX + 27, y + dy + (cellstep * i) + .Header.OffsetY + 16, z)
                        End With

                    Next

                Else
                    dx = 26
                    dy = 8

                    For i As Integer = 0 To 5

                        With Libs.Inventory.Image(arrBeltItems(i).Base.INT_LOOKS)
                            .Draw(x + dx + (cellstep * i) + .Header.OffsetX + 27, y + dy + .Header.OffsetY + 16, z)
                        End With

                    Next

                End If

            ElseIf mode = Globals.DrawingMode.Text Then

                DirectX.ProFont9.DrawBorderText(currentCell, 10, 10, Color.White, Color.Red, z - 0.01)

            End If

        End Sub

        Private Sub btnRotate_Click() Handles btnRotate.Click

            isVertical = Not isVertical

            If isVertical Then

                winBelt.SetSize(50, 274)
                btnClose.SetPos(29, 4)
                btnRotate.SetPos(8, 4)

            Else

                winBelt.SetSize(274, 50)
                btnClose.SetPos(4, 7)
                btnRotate.SetPos(4, 28)

            End If

        End Sub

        Private Sub winBelt_MouseEvent(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs) Handles winBelt.MouseEvent

            If type = mMouseVars.MouseInputType.MouseClick Then

                currentCell = GetCellFromArgs(e)
                AddMouseDragItemToBelt()

            ElseIf type = MouseInputType.MouseDoubleClick Then

                UseBeltItem(GetCellFromArgs(e))

            End If

        End Sub

        Private Function GetCellFromArgs(ByRef e As MouseEventArgs) As Integer

            Dim startx As Integer, starty As Integer

            If isVertical Then

                startx = 9
                starty = 26
                If e.X >= startx And e.X < startx + cellsize Then
                    For i As Integer = 1 To 6

                        If e.Y >= starty And e.Y < starty + cellsize Then
                            Return i
                        Else
                            starty += cellstep
                        End If

                    Next
                End If

            Else
                startx = 26
                starty = 8
                If e.Y >= starty And e.Y < starty + cellsize Then
                    For i As Integer = 1 To 6
                        If e.X >= startx And e.X < startx + cellsize Then
                            Return i
                        Else
                            startx += cellstep
                        End If

                    Next
                End If

            End If

            Return -1

        End Function

        Private Sub btnClose_Click() Handles btnClose.Click
            Hide()
        End Sub

        Private Sub AddMouseDragItemToBelt()

            If GUI.MouseDragItem.HasMouseDragItem Then

                With GUI.MouseDragItem.MouseDragItemDesc.info.Base

                    Select Case .BYTE_TYPE

                        Case ItemType.Potion, ItemType.Scroll
                            arrBeltItems(currentCell - 1) = GUI.MouseDragItem.MouseDragItemDesc.info
                            GUI.MouseDragItem.ReturnMouseDragItem()
                            GUI.MouseDragItem.RemoveMouseDragItem()

                        Case Else
                            MessageBoxes.AddMessageBox("BELTERR", "Cannot put that item on the belt!", mMsgBoxVars.MsgBoxButtons.OK, Nothing)


                    End Select

                End With

            End If

        End Sub

        Public Sub UseBeltItem(idx As Integer)

            If idx = -1 Then

                Dim msg As EvoLib.Protocol.CharChatMsg
                msg.BYTE_TYPE = ChatMsgType.System
                msg.msg.STR_MSG = "No item is assigned to belt " & currentCell & "!"
                ChatBox.ChatIn(msg, DateTime.Now.Ticks)

            End If

            idx -= 1

            With arrBeltItems(idx)

                If .INT_ID > 0 Then

                    Player.UseItem(.INT_ID)

                End If

            End With

        End Sub

    End Module

End Namespace
